﻿<?php

class BuyApp extends BaseApp
{

 
  
    function index()
    {
		
        $id = intval($_GET['id']);
		$gd = intval($_GET['gd']);
	    if($id<=0)
		{
		 $notice = &get_message(array('msg'=>'产品编号错误!','url'=>'','title'=>'Notice'));
		 $notice ->display();				
		}
		
	   if(!LOGIN_ICON)
	   {
		 $notice = &get_message(array('msg'=>'您必须先注册或登陆!','url'=>'','title'=>'Notice'));
		 $notice ->display();			   
		}	
		
		$TB_buy = &$this->_model('buy');
		$data_group = $TB_buy->productcheck($id);



        if(empty($data_group['name']))
		{
		 $notice = &get_message(array('msg'=>'错误~该产品不存在或已结束团购!','url'=>'','title'=>'Notice'));
		 $notice ->display();		
		}		
        $surplusnum ='';
	   if($data_group['maxnum']!=0)
	   {
				
		$data_order = $TB_buy->paytotal($data_group['groupid']);		
		$surplusnum= $data_group['maxnum']-$data_order['payroductnum'];		
		   if($surplusnum<=0)
		   {
		   $notice = &get_message(array('msg'=>'该产品已卖光了，下次请早！','url'=>'','title'=>'Notice'));
		   $notice ->display();				
		   }
	   }

       if(isset($data_group['bookprice'])&&$data_group['bookprice']!=0)
	   {
	   $data_group['nowprice'] = $data_group['bookprice'];
	   }

	   if($surplusnum=='')$surplusnum=999;

        $this->assign('productdata', $data_group);	

		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());
		

		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}			
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);		       
        $this->assign('site_url', SITE_URL);
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/buy.view.html');
    }

    function order()
    {
	   if(!isset($_GET['oid']))
	   {
	    $_GET['oid']='';
	   }

        $id = intval($_POST['id']);		
		$gd = intval($_POST['gd']);
		$num = intval($_POST['num']);
		$oid = $_GET['oid'];
		$city ='';
	    if($id<=0)
		{
		 $notice = &get_message(array('msg'=>'产品编号错误!','url'=>'','title'=>'Notice'));
		 $notice ->display();				
		}
		
	   if(!LOGIN_ICON)
	   {
		 $notice = &get_message(array('msg'=>'您必须先注册或登陆!','url'=>'','title'=>'Notice'));
		 $notice ->display();			   
		}	
		
	  $TB_buy = &$this->_model('buy');

	  if($oid!='')
	  {
				
		$orderdata = $TB_buy->orderget($oid);	
		if(isset($orderdata['id'])){
			$id = $orderdata['productid'];
			$num = $orderdata['productnum'];
			$this->assign('orderdata',$orderdata);
 		}else{
			
			 $notice = &get_message(array('msg'=>'错误~无法找到该订单!','url'=>'','title'=>'Notice'));
		    $notice ->display();		

 		}
	  }

			
	 $orderhavednum = $TB_buy->orderhaved(intval($id),$GLOBALS['SCup']->SCup_user_msg['user_id']);	 
	 $buyed=$orderhavednum['totalorder'];

	 if($buyed)
	 {
			 $notice = &get_message(array('msg'=>'错误~您已经购买过该产品了!','url'=>'','title'=>'Notice'));
		    $notice ->display();		 
	  }


		$data_group = $TB_buy->productcheck($id,$city);

        if(empty($data_group['name']))
		{
		 $notice = &get_message(array('msg'=>'错误~该产品不存在或已结束团购!','url'=>'','title'=>'Notice'));
		 $notice ->display();		
		}		

       if(isset($data_group['bookprice'])&&$data_group['bookprice']!=0)
	   {
	   $data_group['nowprice'] = $data_group['bookprice'];
	   }

	$totalprice=$data_group['nowprice'] * $num;
	

	
	$TB_user = &$this->_model('user');
	$usermoney = $TB_user->moneyuser();
	
	if($usermoney>=$totalprice){
		$totalprice2=0;
	}else{
		$totalprice2 = $totalprice - $usermoney;
	}
	
	$TB_payment = &$this->_model('payment');
	$pay = $TB_payment->paytype();
	$this->assign('pay',$pay);

		$this->assign('num',$num);
			$this->assign('totalprice',$totalprice);	
			$this->assign('usermoney',$usermoney);	
			$this->assign('totalprice2',$totalprice2);				
		$this->assign('groupdata',$data_group);	
		
		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	        
		$this->assign('site_url',SITE_URL);
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/buy_order.view.html');
    }

    function dopay()
    {

        $id = intval($_POST['id']);		
		$gd = intval($_POST['gd']);
		$num = intval($_POST['num']);
		$paytype = intval($_POST['paytype']);
		$city='';
	   if(!LOGIN_ICON)
	   {
		 $notice = &get_message(array('msg'=>'您必须先注册或登陆!','url'=>'','title'=>'Notice'));
		 $notice ->display();			   
		}		
				
	    if($id<=0)
		{
		 $notice = &get_message(array('msg'=>'您还没有选择商品!','url'=>'?','title'=>'Notice'));
		 $notice ->display();				
		}
		

	   if($paytype=='')
	   {
		 $notice = &get_message(array('msg'=>'您没有选择支付方式!','url'=>'?','title'=>'Notice'));
		 $notice ->display();			   
		}		

		$TB_buy = &$this->_model('buy');
		$data_group = $TB_buy->productcheck($id,$city);
		
        if(empty($data_group['name']))
		{
		 $notice = &get_message(array('msg'=>'错误~该产品不存在或已结束团购!','url'=>'?','title'=>'Notice'));
		 $notice ->display();		
		}		
	  
	 
	 $order = $TB_buy->ordergetbyuser(intval($id));	 

	if(isset($order['orderid'])&&$order['orderid'] != '')
	{

		if($order['orderid'] != '' && $order['pay']==1)
		{
		 $notice = &get_message(array('msg'=>'您已经购买过该商品了!','url'=>'?','title'=>'Notice'));
		 $notice ->display();			

		}else
		{
			$ary=array(
			'paytype' => $paytype,
			'productnum' => $num,
			);
			
		 $TB_buy->orderedit($order['orderid'],$ary);
		 $notice = &get_message(array('msg'=>'正在为您跳转到付款页面','url'=>"?app=buy&act=pay&oid=".$order['orderid'],'title'=>'Notice'));
		 $notice ->display();				
			
		}
	}

	$orderid=$TB_buy->getorderid();

	
	$ary=array(
		'orderid' => $orderid,
		'groupid' => $data_group['groupid'],		
		'productid' => $id,
		'productnum' => $num,
		'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
		'buytime' => time(),
		'paytype' => $paytype,
		'pay' => 0,
		'status' => 1
	);
	
	$TB_buy->ordercreater($ary);
	
		 $notice = &get_message(array('msg'=>'正在为您跳转到付款页面','url'=>"?app=buy&act=pay&oid=".$orderid,'title'=>'Notice'));
		 $notice ->display();			


	
    }



    function pay()
    {
        $oid = $_GET['oid'];		
		$city='';
	   if(!LOGIN_ICON)
	   {
		 $notice = &get_message(array('msg'=>'您必须先注册或登陆!','url'=>'','title'=>'Notice'));
		 $notice ->display();			   
		}		
				
	    if($oid=='')
		{
		 $notice = &get_message(array('msg'=>'无法继续付款，无法找到订单!','url'=>'?','title'=>'Notice'));
		 $notice ->display();				
		}
		
	   $TB_buy = &$this->_model('buy');	  

	 
	   $order = $TB_buy->orderget($oid,$GLOBALS['SCup']->SCup_user_msg['user_id']);
   
     	if(!isset($order['orderid'])&&$order['orderid'] != '')
		{
		 $notice = &get_message(array('msg'=>'无法继续付款，无法找到订单!','url'=>'?','title'=>'Notice'));
		 $notice ->display();	
		}

		$data_group = $TB_buy->productcheck(intval($order['productid']),$city);


       if(isset($data_group['bookprice'])&&$data_group['bookprice']!=0)
	   {
	   $data_group['nowprice'] = $data_group['bookprice'];
	   }


        if(empty($data_group['name']))
		{
		 $notice = &get_message(array('msg'=>'订单不存在或产品已过期!','url'=>'?','title'=>'Notice'));
		 $notice ->display();		
		}	
		
	   $price=$order['productnum']*$data_group['nowprice'];

	$TB_user = &$this->_model('user');
	$usermoney = $TB_user->moneyuser();

	if($usermoney>=$price){
		$this -> actpay('0',$oid);
		exit;
	}else{
		$price = $price - $usermoney;
	}

	$TB_payment = &$this->_model('payment');
	$pay = $TB_payment->paychoose(intval($order['paytype']));

	$pay['orderid'] = $order['orderid'];
	$pay['price']=$price;
	$pay['name']=$data_group['name'];
	

	if($pay['is_cod']!=1)
	{

			$returnurl = SITE_URL.'/?app=buy&act=repay';

			$bottom=$this->topay($pay['pay_code'],$returnurl,$pay);
           $this->assign('bottom', $bottom);			
	}
	$pay['pay_desc']=nl2br($pay['pay_desc']);
	
        $this->assign('pay', $pay);
        $this->assign('order',  $order);
        $this->assign('price',  $price);		
        $this->assign('orderid',  $oid);		
		
		require(ROOT_PATH . '/lib/nav.lib.php');
		
		$nav = new Nav($GLOBALS['SCup']->SCup_nav_file);
		
        $this->assign('navigation', $nav->_get_nav());	
        $this->assign('login_icon', $GLOBALS['SCup']->SCup_user_checking);	
		if(isset($GLOBALS['SCup']->SCup_user_msg['data']['user_name']))
		{
        $this->assign('username',$GLOBALS['SCup']->SCup_user_msg['data']['user_name']);			
		}	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);		
		$this->assign('bg_pic_url',$GLOBALS['SCup']->SCup_name.'/view/'.VIEW_TYPE .'/style/'.CSS_TYPE.'/');
		        
		$this->assign('site_url',SITE_URL);
        $this->assign('langtype', trim($GLOBALS['Lang']->_lang_tp));
		
        $this->display('group/buy_pay.view.html');
    }


function Repay(){
	$pay_code = !empty($_REQUEST['pay']) ? trim($_REQUEST['pay']) : '';
	include_once(ROOT_PATH . '/lib/pay/'.$pay_code.'.pay.php');
    $app_pay_class_name = ucfirst($pay_code);
    $app_pay     = new $app_pay_class_name();
	$msg=$app_pay->payRe();

	if(is_array($msg)){
		$this -> actpay($msg['price'],$msg['orderid']);
	}else{

		 $notice = &get_message(array('msg'=>'$msg','url'=>'?','title'=>'Notice'));
		 $notice ->display();	
	}
	exit;
}

function topay($app,$returnurl,$pay)
{
	$payment=unserialize($pay['pay_config']);
	$returnurl.='&pay='.$app;
	include_once(ROOT_PATH . '/lib/pay/'.$app.'.pay.php');
    $app_pay_class_name = ucfirst($app);
    $app_pay     = new $app_pay_class_name();
	$bottom=$app_pay->payTo($payment,$returnurl,$pay);
	return $bottom;
	exit;
}


 function actpay($price,$orderid)
 {

 	if($price=='' || $orderid=='')
	{
		 $notice = &get_message(array('msg'=>'首页验证支付失败!','url'=>'?','title'=>'Notice'));
		 $notice ->display();	

	}
	if($price>0)
	{
		$price = float($price);
			$TB_user = &$this->_model('user');
	        $pay = $TB_user->moneyadd($price,$GLOBALS['SCup']->SCup_user_msg['user_id']);

            $ary=array(
				'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
				'type' => 1,
				'name' => '充值账户',
				'intro' => '您为账户充值'.$price.'元',
				'money' => $price,
				'time' => time(),
		   );
		$TB_user->moneylog($ary);
	};
	
	$nowdate=date('Y-m-d',time());
	

	   $TB_buy = &$this->_model('buy');	  

	 
	   $order = $TB_buy->ordercheck($orderid);	
	
	if($order['orderid'] == '')
	{
		 $notice = &get_message(array('msg'=>'错误~该订单已经支付或不存在!','url'=>'?','title'=>'Notice'));
		 $notice ->display();			
	}
	
	 $order = $TB_buy->orderget($orderid,$GLOBALS['SCup']->SCup_user_msg['user_id']);
$city ='';

		$product = $TB_buy->productcheck($order['productid'],$city);


        if(empty($product['name']))
		{
		 $notice = &get_message(array('msg'=>'错误~该产品不存在或已结束团购!','url'=>'?','title'=>'Notice'));
		 $notice ->display();		
		}

	
	if($product['maxnum']!=0)
	{
		$data_order = $TB_buy->paytotal($product['groupid']);
		
		$surplusnum=$data_order['payroductnum'];

		if($surplusnum>$product['maxnum'])
		{
			
		 $notice = &get_message(array('msg'=>'该产品被人抢先购买了，您的款项已经返还到个人账户！','url'=>'?','title'=>'Notice'));
		 $notice ->display();			

		}
	}

       if(isset($product['bookprice'])&&$product['bookprice']!=0)
	   {
	   $product['nowprice'] = $product['bookprice'];
	   }



	$price=$product['nowprice']*$order['productnum'];
	

	$TB_user = &$this->_model('user');
	$usermoney = $TB_user->moneyuser();	
	
	if($price<=$usermoney )
	{
		$TB_user->moneypay($price,$GLOBALS['SCup']->SCup_user_msg['user_id']);
		$TB_buy->addsellertomoney($product['sellerid'],$price);
	}
	else
	{
		 $notice = &get_message(array('msg'=>'您的余额不足，不够支付本次消费！','url'=>'?','title'=>'Notice'));
		 $notice ->display();		

	}
	
	$result = $TB_buy->ordertype($orderid,1,1,1);
	$this -> ticket($orderid);


 }



function ticket($orderid)
    {
		
	   $TB_buy = &$this->_model('buy');		 
	   $order = $TB_buy->orderticket($orderid);	

		if($order=='')
		{
		 $notice = &get_message(array('msg'=>'错误，无法找到类似订单！','url'=>'?','title'=>'Notice'));
		 $notice ->display();		
		}

       if(isset($order['bookprice'])&&$order['bookprice']!=0)
	   {
	   $order['nowprice'] = $order['bookprice'];
	   }

		$ary=array(
			'userid' => $GLOBALS['SCup']->SCup_user_msg['user_id'],
			'type' => 0,
			'name' => '团购商品',
			'intro' => '购买了商品['.$order['name'].'] 单价：'.$order['nowprice'].'元     一共购买了'.$order['productnum'].'个',
			'money' => $order['nowprice']*$order['productnum'],
			'time' => time(),
		);
	
	
			$TB_user = &$this->_model('user');
		   $TB_user->moneylog($ary);
		
			
		$product = $TB_buy->paytotal($order['groupid']);
		
		$num=$product['totalorder'];
		if($num<$order['successnum'])
		{
		 $notice = &get_message(array('msg'=>'您已经成功团购该产品，但团购差'.($order['successnum']-$num).'人达成 ，快去邀请好友参加团购，还能获得丰厚获取奖励!','url'=>'?','title'=>'Notice'));
		 $notice ->display();	
		}
		
		if($num==$order['successnum'])
		{
			$orderPayed=$TB_buy->orderPaylist($order['groupid']);

			foreach($orderPayed as $key => $value)
			{
                
				$TB_buy->producttypedit($value['productid'],2);
				$ary=array(
				'address'=> $value['email'],
				'username'=>$value['username'],
				'title'=>'团购成功提示信息',
				'content'=>'恭喜您在本站团购的产品('.$order['name'].')在'.date('Y-m-d H:i',time()).'团购成功，请点<a href="'.SITE_URL.'">这里</a>查看您的团购券！',
				'addtime'=>time()
				);

		        $TB_user->mailCron($ary);

				for($i=1;$i<=$value['productnum'];$i++)
				{
					$result=$TB_user->ticketCreate($value['userid'],$value['productid'],$order['orderid'],$order['groupid']);
					if($result=='')$i--;
				}
			}

		    $notice = &get_message(array('msg'=>'团购成功，您还可以推荐您的朋友来购买哦！','url'=>'?','title'=>'Notice'));
		    $notice ->display();	

		}

		
		$TB_buy->producttypedit($order['productid'],2);
		for($i=1;$i<=$order['productnum'];$i++)
		{
			$result=$TB_user->ticketCreate($GLOBALS['SCup']->SCup_user_msg['user_id'],$order['productid'],$order['orderid'],$order['groupid']);
			if($result=='')$i--;
		}
	    $notice = &get_message(array('msg'=>'团购成功，您还可以推荐您的朋友来购买哦！','url'=>'?','title'=>'Notice'));
	    $notice ->display();


	}

}
?>